﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Animoso.Domain;

namespace Animoso.Web.Binders
{
    public class RegistrationBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Registration registration = new Registration();
            
            Address address = new Address();
            address.City = GetValueFromControllerContext(controllerContext, "City");
            address.Number = GetValueFromControllerContext(controllerContext, "Number");
            address.Street = GetValueFromControllerContext(controllerContext, "Street");
            address.ZipCode = GetValueFromControllerContext(controllerContext, "Zipcode");

            registration.Address = address;
            registration.BankAccount = GetValueFromControllerContext(controllerContext, "BankAccount");
            registration.BankOwnerName = GetValueFromControllerContext(controllerContext, "BankOwnerName");
            registration.DateOfBirth = DateTime.Parse(GetValueFromControllerContext(controllerContext, "DateOfBirth"));
            registration.Education = GetValueFromControllerContext(controllerContext, "Education");
            registration.Email = GetValueFromControllerContext(controllerContext, "Email");
            registration.FirstName = GetValueFromControllerContext(controllerContext, "FirstName");
            registration.IsBecomeMember =
                Boolean.Parse(GetValueFromControllerContext(controllerContext, "IsBecomeMember"));
            registration.IsParticipant = Boolean.Parse(GetValueFromControllerContext(controllerContext, "IsParticipant"));
            registration.LastName = GetValueFromControllerContext(controllerContext, "LastName");
            registration.MobilePhone = GetValueFromControllerContext(controllerContext, "MobilePhone");
            
            Address parentalAddress = new Address();
            parentalAddress.City = GetValueFromControllerContext(controllerContext, "ParentCity");
            parentalAddress.Number = GetValueFromControllerContext(controllerContext, "Parentnumber");
            parentalAddress.Street = GetValueFromControllerContext(controllerContext, "ParentStreet");
            parentalAddress.ZipCode = GetValueFromControllerContext(controllerContext, "ParentZipcode");

            registration.ParentalAddress = parentalAddress;

            registration.ParentalPhone = GetValueFromControllerContext(controllerContext, "ParentPhone");
            registration.Phone = GetValueFromControllerContext(controllerContext, "Phone");

            return registration;
        }

        private string GetValueFromControllerContext(ControllerContext controllerContext, string key)
        {
            return controllerContext.HttpContext.Request.Form[key];
        }
    }
}
